package com.zc.im.controller;

import com.zc.im.model.im.group.ZcImMsgHistoryModel;
import com.zc.im.model.im.group.ZcImUnreadNum;
import com.zc.im.model.user.*;
import com.zc.im.service.im.group.ImMsgHistoryService;
import com.zc.im.service.im.group.ImUnreadNumService;
import com.zc.im.service.user.UserService;
import com.zc.im.service.user.ZcAdminUseService;
import com.zc.im.service.user.ZcComInfoService;
import com.zc.im.service.user.ZcOwnWorkService;
import com.zc.im.util.ImGolbalTypeContext;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.SpringContextUtils;
import com.zc.im.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/15.
 */

@Controller
@RequestMapping("/unreadNum")
public class ImUnreadNumController {

    @Autowired
    public ImUnreadNumService unreadNumService;

    @Autowired
    public UserService userService;

    @Autowired
    public ImMsgHistoryService msgHistoryService;

    @Autowired
    public ZcAdminUseService adminUseService;


    /**
     * 更新未读数至默认
     * 2017-7-15 19:54:16
     * @param userId
     * @param targetId
     * @param type
     * @return
     */
    @RequestMapping("/updateUnreadNumToDef")
    @ResponseBody
    public ResponseJson updateUnreadNumToDef(Integer userId, Integer targetId, String type){
        ResponseJson responseJson = new ResponseJson();
        if(userId == null || targetId == null || StringUtils.isEmpty(type)){
            return responseJson;
        }
        Map<String, Object> backMap = new HashMap<>();
        Integer updateFlag = unreadNumService.updateUnreadNumToDef(userId, targetId, type);

        backMap.put("updateFlag", updateFlag);
        responseJson.setDataMap(backMap);
        return responseJson;
    }


    /**
     * 查询未读数列表
     * 2017-7-15 20:03:19
     * @param userId
     * @param type
     * @return
     */
    @RequestMapping("/queryUnreadNumByUser")
    @ResponseBody
    public ResponseJson queryUnreadNumByUser(Integer userId, String type, Integer pageSize, Integer pageNum){
        ResponseJson responseJson = new ResponseJson();

        List<UserImModel> unReadImInfo = new ArrayList<>();
        List<UserImModel> historyImInfo = new ArrayList<>();
        List<UserImModel> unReadimModels = new ArrayList<>();

        if(userId == null ){
            Map<String, Object> dataMap = setUnreadNumMap(unReadImInfo, historyImInfo);
            responseJson.setDataMap(dataMap);
            return responseJson;
        }

        //未读消息数量
        List<ZcImUnreadNum> unReadImNums =  unreadNumService.queryUnreadNumByUser(userId, type, 1);
        if(unReadImNums != null && unReadImNums.size() > 0){
            Integer readFlag = 0;
            unReadimModels = setUnreadInfos(unReadImNums, readFlag);
        }
        List<ZcImUnreadNum> historyIm = unreadNumService.queryUnreadNumByUser(userId, type, 0);
        Integer readFlag = 1;
        historyImInfo = setUnreadInfos(historyIm, readFlag);
        Map<String, Object> dataMap = setUnreadNumMap(unReadimModels, historyImInfo);
        responseJson.setDataMap(dataMap);
        return responseJson;
    }



    public Map<String, Object> setUnreadNumMap(List<UserImModel> unreadNums, List<UserImModel> historyIm){
        Map<String, Object> dataMap = new HashMap<>();
        if(unreadNums == null || unreadNums.size() == 0){
            dataMap.put("unreadNums", new ArrayList<UserImModel>());
        }else{
            dataMap.put("unreadNums", unreadNums);
        }
        if(historyIm == null || historyIm.size() == 0){
            dataMap.put("historyIm", new ArrayList<UserImModel>());
        }else{
            dataMap.put("historyIm", historyIm);
        }

        return dataMap;
    }


    /**
     * 组装未读消息数量
     * 2017-7-16 13:56:36
     * @param readFlag
     * @param unreadNums
     * @return
     */
    public List<UserImModel> setUnreadInfos(List<ZcImUnreadNum> unreadNums, Integer readFlag){
        List<UserImModel> imModels = new ArrayList<>();

        if(unreadNums == null || unreadNums.size() ==0){
            return imModels;
        }


        //用户信息
        for(ZcImUnreadNum unreadNum: unreadNums){
            UserImModel userImModel = new UserImModel();
            //客服
            if(ImGolbalTypeContext.IM_MSG_TYPE_CUSTOMER_SERVICE.equals(unreadNum.getSendUserType())){
                ZcAdminUser zcAdminUser = adminUseService.findZcAdminUser(unreadNum.getSendUserId());
                if(zcAdminUser == null){
                    continue;
                }
                userImModel.setUserId(zcAdminUser.getUid());
                userImModel.setPhone(zcAdminUser.getPhone());
                userImModel.setNickName(zcAdminUser.getUserName());
                if(StringUtils.isEmpty(zcAdminUser.getPicHead())){
                    userImModel.setPicHead("http://www.tazhaowo.com/Public/img/index/com_logo.png");
                }else{
                    userImModel.setPicHead(zcAdminUser.getPicHead());
                }
            }
            //用户
            if(ImGolbalTypeContext.IM_MSG_TYPE_PEOPLE.equals(unreadNum.getSendUserType())){
                ZcUser user = userService.findUser(unreadNum.getSendUserId());
                if(user == null){
                    continue;
                }
                ZcComInfoService zcOwnWorkService = (ZcComInfoService) SpringContextUtils.getBeanByClass(ZcComInfoService.class);
                ZcComInfo zcComInfo = zcOwnWorkService.findZcComInfoId(unreadNum.getSendUserId());
                if (zcComInfo != null) {
                //插入职位
                userImModel.setPosition(zcComInfo.getRegisterJob());
                //插入公司名称
                userImModel.setCompany_name(zcComInfo.getCompanyName());
                }
                userImModel.setUserId(user.getUid());
                userImModel.setPhone(user.getPhone());
                userImModel.setType(user.getType().intValue());
                if(StringUtils.isEmpty(user.getNickname())){
                    userImModel.setNickName("匿名用户");
                }else{
                    userImModel.setNickName(user.getNickname());
                }
                if(StringUtils.isEmpty(user.getPicHead())){
                    userImModel.setPicHead("http://www.tazhaowo.com/Public/img/index/background.png");
                }else{
                    userImModel.setPicHead(user.getPicHead());
                }
            }
            //未读数量
            userImModel.setUnreadNum(unreadNum.getUnreadNum());
            //未读消息数量聊天最后10条记录
            ZcImMsgHistoryModel unreadHistoryCriteria = new ZcImMsgHistoryModel();
            unreadHistoryCriteria.setTargetObjId(unreadNum.getTargetObjId());
            unreadHistoryCriteria.setSourceObjId(unreadNum.getSendUserId());
            List<ZcImMsgHistoryModel> unreadHistorys = msgHistoryService.selectImMsgHistorys(
                    unreadHistoryCriteria, ImGolbalTypeContext.PAGEHELPER_PAGENUM , ImGolbalTypeContext.PAGEHELPER_PAGESIZE);
            userImModel.setHistoryModels(unreadHistorys);

            imModels.add(userImModel);
        }
        return imModels;
    }



}
